package com.ysj.demo.accompanynetwork.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.ysj.demo.accompanynetwork.R;
import com.ysj.demo.accompanynetwork.bean.BaseBean;
import com.ysj.demo.accompanynetwork.bean.VerificationBean;
import com.ysj.demo.accompanynetwork.mvp.presenter.RegisterPresenter;
import com.ysj.demo.accompanynetwork.mvp.view.RegisterView;
import com.ysj.demo.accompanynetwork.util.StatusBarUtil;
import com.ysj.demo.accompanynetwork.util.ToolUtil;

public class RegisterActivity extends BaseActivity implements RegisterView, View.OnClickListener {
    private RegisterPresenter registerPresenter;
    private TextView titleTv, desTv;
    private EditText nickEt, passwordEt, phoneEt, codeEt;
    private ImageView codeIv;
    private Button registerBtn;

    private VerificationBean body;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        StatusBarUtil.fullScreen(this);

        initView();
        initListener();
    }

    private void initView() {
        registerPresenter = new RegisterPresenter();
        registerPresenter.bind(RegisterActivity.this);
        titleTv = findViewById(R.id.top_tv);
        titleTv.setText(getResources().getString(R.string.register));
        desTv = findViewById(R.id.des_tv);
        nickEt = findViewById(R.id.nick_et);
        passwordEt = findViewById(R.id.password_et);
        phoneEt = findViewById(R.id.phone_et);
        codeEt = findViewById(R.id.verification_code_et);
        codeIv = findViewById(R.id.verification_code_iv);
        registerBtn = findViewById(R.id.register_btn);

        //3，实现部分字体颜色的改变，并能点击 我已阅读并同意《陪玩网服务使用协议》
        String str_3 = "我已阅读并同意";
        String str_4 = "《陪玩网服务使用协议》";
        //这里无论是使用  SpannableString 还是  SpannableStringBuilder 都一样
        SpannableString ss = new SpannableString(str_4);
//        SpannableStringBuilder s = new SpannableStringBuilder(str_4);
        MyClickableSpan clickSpan = new MyClickableSpan(this, str_4);
        ss.setSpan(clickSpan, 0, str_4.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        desTv.setText(str_3);
        desTv.append(ss);
        //必须加这一句，否则就无法被点击
        desTv.setMovementMethod(LinkMovementMethod.getInstance());
    }

    private void initListener() {
        codeIv.setOnClickListener(this);
        registerBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //获取验证码
            case R.id.verification_code_iv:
                registerPresenter.getVerifyCode();
                break;
            //注册
            case R.id.register_btn:
                registerPresenter.register();
                break;
        }
    }

    @Override
    public String getNick() {
        return nickEt.getText().toString().trim();
    }

    @Override
    public String getPassword() {
        return passwordEt.getText().toString().trim();
    }

    @Override
    public String getPhone() {
        return phoneEt.getText().toString().trim();
    }

    @Override
    public String getCode() {
        return codeEt.getText().toString().trim();
    }

    @Override
    public void setCodeBody(VerificationBean bean) {
        body = bean;
        byte[] bitmapArray = Base64.decode(body.getImageBytes(), Base64.DEFAULT);
        //  Bitmap bitmap= BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);
        Glide.with(getApplicationContext()).load(bitmapArray).into(codeIv);
        Log.i("RegisterActivity", bitmapArray.length + "  " + body.getImageBytes());
    }

    @Override
    public VerificationBean getCodeBody() {
        return body;
    }

    @Override
    public void OnRegisterSuccess(BaseBean body) {
        switch (body.getCode()) {
            case 0:
                ToolUtil.ToastShow(getApplication(), "注册成功");
                Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                startActivity(intent);
                break;
            default:
                ToolUtil.ToastShow(getApplication(), "注册失败");
                break;
        }
    }

    @Override
    public void showVerifyFailed(String msg) {
        ToolUtil.ToastShow(getApplication(), msg);
    }

    /**
     * 这个类 实际上和第一种改变颜色的方法差不多，只不过 那是个专门改变颜色的Span，这是个专门负责点击处理的Span
     *
     * @author Administrator
     */
    class MyClickableSpan extends ClickableSpan {
        private Context context;
        private String text;

        public MyClickableSpan(Context context, String text) {
            this.context = context;
            this.text = text;
        }

        //在这里设置字体的大小，等待各种属性
        @Override
        public void updateDrawState(TextPaint ds) {
            ds.setColor(Color.RED);
        }

        @Override
        public void onClick(View widget) {
            Intent intent = new Intent(RegisterActivity.this, AgreementActivity.class);
            startActivity(intent);
        }
    }
}
